<?php

namespace App\Admin\Controllers;

use App\Models\Org;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Grid\Displayers\Actions;

class OrgController extends AdminController
{
    protected $title = '组织机构管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Org());

        $grid->title('组织机构名称')->style('min-width:100px;');
        $grid->column('type', '机构性质')->using(['0' => '政府机关', '1' => '企业'])->sortable()->style('min-width:100px;');
//        $grid->column('parentOrg.title', '所属单位')->style('min-width:100px;');
        $grid->people('联系人')->style('min-width:100px;');
        $grid->mobile('联系方式')->style('min-width:100px;');
        $grid->column('status', '状态')->using(['0' => '禁用', '1' => '启用'], '未知')->dot([
            0 => 'danger',
            1 => 'success',
        ], 'warning')->sortable()->style('min-width:100px;');

        $grid->column('created_at', '创建时间')->sortable()->style('min-width:100px;');

        $grid->disableExport();
        $grid->disableRowSelector();


        $grid->filter(function (Grid\Filter $filter) {
            $filter->expand();
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->like('title', '组织机构名称');
        });

        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });

        $grid->setActionClass(Actions::class);

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Org::findOrFail($id));

        $show->field('title', '组织机构名称');
        $show->field('type', '机构性质');
        $show->field('people', '联系人');
        $show->field('mobile', '联系方式');
        $show->field('address', '地址');
        $show->field('status', '状态');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Org());

        $form->display('id', 'ID');

        $form->text('title', '组织机构名称')->rules('required');
//        $form->radio('type', '机构性质')->options(['0' => '政府机关', '1' => '企业'])->default('0');

        if($form->model()->id != 1){
            $form->radioButton('type', '机构性质')
                ->options([
                    0 => '政府机关',
                    1 => '企业',
                ])->when(0, function (Form $form) {
                    $form->select('parent_id', '所属单位')->options(Org::where('type', 0)->where('id', '=', 1)->pluck('title', 'id'))->default(1)->rules('required');
                })->when(1, function (Form $form) {
                    $form->select('parent_id', '所属单位')->options(Org::where('type', 0)->where('id', '!=', 1)->pluck('title', 'id'))->rules('required');
                })->default(1)->rules('required');
        }

        $form->text('people', '联系人');
        $form->text('mobile', '联系方式');
        $form->text('address', '地址');



        $form->radio('status', '状态')->options(['0' => '禁用', '1' => '启用'])->default('1');

        $form->saved(function (Form $form) {
            $parentOrg = Org::find($form->model()->parent_id);

            !empty($parentOrg) && Org::where('id', $form->model()->id)->update([
                'path' => $parentOrg->path.'/'.$form->model()->id,
            ]);
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });

        return $form;
    }
}
